﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Coordinate
namespace backoffice.Data
{
    partial class Coordinate
    {
        public void setCoordinate(Coordinate c)
        {
            this.Latitude = c.Latitude; 
            this.Longitude = c.Longitude;
            this.CoordinateID = c.CoordinateID;
            this.CountryID = c.CountryID;
            this.RegionID = c.RegionID;
            this.ZoneID = c.ZoneID;
        }
    }
}
#endregion

#region dataCoordinate
namespace backoffice.Data
{
    public class dataCoordiante
    {
        #region Create
        public static void createCoordinate(Coordinate c)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                c.CoordinateID = Guid.NewGuid();

                db.Coordinates.InsertOnSubmit(c);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Coordinate getCoodinate(Guid guid)
        {
            try
            {
                if (guid == null) return null;

                db2growDataContext db = new db2growDataContext();

                Coordinate c = (from co in db.Coordinates
                                where co.CoordinateID == guid
                                select co).FirstOrDefault();

                return c;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Coordinate> getCoordinates(Expression<Func<Coordinate, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Coordinates.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeCoordinate(Coordinate c)
        {
            try
            {
                if (c == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Coordinate res = db.Coordinates.FirstOrDefault(co => co.CoordinateID == c.CoordinateID);

                    if (res == null) return false;
                    else
                    {
                        res.setCoordinate(c);
                        db.SubmitChanges();
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteCoordinate(Coordinate c)
        {
            try
            {
                if (c == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    if(db.Coordinates.Contains(c))
                    {
                        Coordinate res = (from co in db.Coordinates
                                          where co.CoordinateID == c.CoordinateID
                                          select co).FirstOrDefault();

                        if(res == null) return false;
                        else
                        {
                            db.Coordinates.DeleteOnSubmit(res);
                            db.SubmitChanges();
                            return true;
                        }
                    }
                    else{
                        return false;
                    }
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion